
package com.dps.omc.action;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dps.omc.model.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dps.common.domain.view.BizData4Page;
import com.dps.omc.model.TaskNode;
import com.dps.omc.service.ITaskNodeService;
import com.dps.common.utils.IdGenerator;
import com.dps.common.utils.JsonResult;
import com.dps.common.utils.PageParam;
import com.dps.common.utils.Constants;

@Controller
public class TaskNodeAction extends AbstractAdminController<ITaskNodeService>{

    @Autowired
    private ITaskNodeService taskNodeService;
    
    @RequestMapping(value="/tasknode/list")
    public String list(TaskNode taskNode,ModelMap model,PageParam pageParam,HttpServletRequest request,HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(taskNode); 	
    	BizData4Page<TaskNode> pageCtx = doPage(model, conditions, pageParam);
    	model.addAttribute("taskNode", taskNode);
    	return "/module/tasknode/list";
    }    
    
    @RequestMapping(value="/tasknode/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

    	model.addAttribute("taskNode", new TaskNode());
        return "module/tasknode/edit";
    }    
    
    @RequestMapping(value="/tasknode/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
    	
    	TaskNode taskNode = taskNodeService.fetch(id);
    	model.addAttribute("taskNode", taskNode);
        return "module/tasknode/edit";
    }    
    
    @RequestMapping(value="/tasknode/save")
    @ResponseBody
    public String save(HttpServletRequest request,TaskNode taskNode){
		boolean isAddNew = StringUtils.isBlank(taskNode.getId())?true:false;
        User user=getCurrentUser();
		if(isAddNew)
		{
			taskNode.setId(IdGenerator.createNewId());
            taskNode.setCreaterId(user.getId());
            taskNode.setCreaterName(user.getRealName());
			taskNodeService.add(taskNode);
		}
		else
		{
			taskNodeService.update(taskNode);
		}
        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }    
    
    @RequestMapping(value="/tasknode/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	taskNodeService.deleteByIds(ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }

    protected Map getQueryMap(TaskNode taskNode)
    {
        Map<String, Object> conditions = new HashMap();

        conditions.put("nodeId", taskNode.getNodeId());
        conditions.put("ip", taskNode.getIp());
        conditions.put("bizType", taskNode.getBizType());
        conditions.put("groupType", taskNode.getGroupType());
        conditions.put("opSystem", taskNode.getOpSystem());
        conditions.put("bizUnit", taskNode.getBizUnit());
        conditions.put("sysRank", taskNode.getSysRank());
        conditions.put("alias", taskNode.getAlias());
        conditions.put("jobId", taskNode.getJobId());
        conditions.put("isTimer", taskNode.getIsTimer());
        conditions.put("taskId", taskNode.getTaskId());
        conditions.put("planDate", taskNode.getPlanDate());
        conditions.put("createDate", taskNode.getCreateDate());
        conditions.put("createrId", taskNode.getCreaterId());
        conditions.put("createrName", taskNode.getCreaterName());
        return conditions;
    }

    @Override
    protected ITaskNodeService getMainService() {
        return taskNodeService;
    }

    @Override
    protected String getMainObjName() {
        return "tasknode";
    }

    @Override
    protected String getViewTitle() {
        return "tasknode";
    }
}
